import axios from 'axios'
import router from '../router/index'

const http = axios.create({
    timeout: 3000,
    baseURL: process.env.NODE_ENV == 'development' ? 'http://localhost:3001/web/api' : '/web/api'
})

http.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token') || ''
        config.headers.Authorization = token
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

http.interceptors.response.use(
    response => {
        return Promise.resolve(response)
    },
    error => {
        console.log(error.response);
        
        if(error.response.status){
            // switch (err.response.status){
            //     case 401: router.replace({ path: '/login'})
            // }
            if(error.response.status == 401){
                localStorage.removeItem('token')
                router.replace({ path: '/login'})
            }
        }
    }
)

export default http